//
//  NSDataExtension.swift
//  DragonWallet
//
//  Created by volantPig on 2024/2/28.
//

import UIKit
import CommonCrypto

class NSDataExtension: NSObject {

}

extension NSData {
    func hexedString() -> String
    {
        var string = String()
        let unsafePointer = bytes.assumingMemoryBound(to: UInt8.self)
        for i in UnsafeBufferPointer<UInt8>(start:unsafePointer, count: length)
        {
            string += Int(i).hexedString()
        }
        return string
    }
    func MD5() -> NSData
    {
        let result = NSMutableData(length: Int(CC_MD5_DIGEST_LENGTH))!
        let unsafePointer = result.mutableBytes.assumingMemoryBound(to: UInt8.self)
        CC_MD5(bytes, CC_LONG(length), UnsafeMutablePointer<UInt8>(unsafePointer))
        return NSData(data: result as Data)
    }
}

extension Int
{
    func hexedString() -> String
    {
        return NSString(format:"%02x", self) as String
    }
}
